package com.flipdf2.serialize.ppml.element;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLRequiredResources {
	public static final String NAME = "REQUIRED_RESOURCES";
	
	private List<PPMLFont> fontList;
	private List<PPMLExternalData> externalDataList;
	private List<PPMLProcessor> processorList;
	private List<PPMLSuppliedResourceRef> suppliedResourceRefList;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (fontList != null && fontList.size() > 0) {
			for (int i = 0, n = fontList.size(); i < n; i++) {
				root.addContent(fontList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (externalDataList != null && externalDataList.size() > 0) {
			for (int i = 0, n = externalDataList.size(); i < n; i++) {
				root.addContent(externalDataList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (processorList != null && processorList.size() > 0) {
			for (int i = 0, n = processorList.size(); i < n; i++) {
				root.addContent(processorList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (suppliedResourceRefList != null && suppliedResourceRefList.size() > 0) {
			for (int i = 0, n = suppliedResourceRefList.size(); i < n; i++) {
				root.addContent(suppliedResourceRefList.get(i).toXML(defaultNamespace));
			}
		}
		return root;
	}
	
	public void check() throws PPMLException {
	}

	public List<PPMLExternalData> getExternalDataList() {
		return externalDataList;
	}

	public void setExternalDataList(List<PPMLExternalData> externalDataList) {
		this.externalDataList = externalDataList;
	}
	
	public void addExternalData(PPMLExternalData externalData) {
		if (this.externalDataList == null)
			this.externalDataList = new ArrayList<PPMLExternalData>();
		this.externalDataList.add(externalData);
	}

	public List<PPMLFont> getFontList() {
		return fontList;
	}

	public void setFontList(List<PPMLFont> fontList) {
		this.fontList = fontList;
	}
	
	public void addFont(PPMLFont font) {
		if (this.fontList == null)
			this.fontList = new ArrayList<PPMLFont>();
		this.fontList.add(font);
	}

	public List<PPMLProcessor> getProcessorList() {
		return processorList;
	}

	public void setProcessorList(List<PPMLProcessor> processorList) {
		this.processorList = processorList;
	}
	
	public void addProcessor(PPMLProcessor processor) {
		if (this.processorList == null)
			this.processorList = new ArrayList<PPMLProcessor>();
		this.processorList.add(processor);
	}

	public List<PPMLSuppliedResourceRef> getSuppliedResourceRefList() {
		return suppliedResourceRefList;
	}

	public void setSuppliedResourceRefList(List<PPMLSuppliedResourceRef> suppliedResourceRefList) {
		this.suppliedResourceRefList = suppliedResourceRefList;
	}
	
	public void addSuppliedResource(PPMLSuppliedResourceRef suppliedResource) {
		if (this.suppliedResourceRefList == null)
			this.suppliedResourceRefList = new ArrayList<PPMLSuppliedResourceRef>();
		this.suppliedResourceRefList.add(suppliedResource);
	}
}
